<template>
  <div class="loginBoxs">
    <div class="topback">找回密码</div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          :rules="rules"
          label-position="left"
          label-width="0px"
          class="login-form"
        >
          <el-form-item prop="username">
            <el-input
              v-model="ruleForm.username"
              type="text"
              maxlength="11"
              auto-complete="off"
              placeholder="请输入手机号"
              class="iptHeight"
            >
              <template #prefix>
                <icon icon="icon-phone" />
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-input
                v-model="ruleForm.verificationCode"
                maxlength="6"
                type="text"
                class="iptHeight graphic"
                auto-complete="off"
                placeholder="请输入图形验证码"
            >
              <template #prefix>
                <icon icon="icon-code" />
              </template>
              <template #append>
                <img :src="graphicCode" @click="loadImage" />
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="code">
            <el-input
              v-model="ruleForm.code"
              maxlength="6"
              type="text"
              class="iptHeight"
              auto-complete="off"
              placeholder="请输入验证码"
            >
              <template #prefix>
                <icon icon="icon-code" />
              </template>
              <template #append>
                <VerificationCode
                    :phone="ruleForm.username"
                    :verificationCode="ruleForm.verificationCode"
                />
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="ruleForm.password"
              type="password"
              maxlength="16"
              auto-complete="off"
              placeholder="请输入密码"
              class="iptHeight"
            >
              <template #prefix>
                <icon icon="icon-password" />
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="newPassword">
            <el-input
              v-model="ruleForm.newPassword"
              type="password"
              maxlength="16"
              auto-complete="off"
              placeholder="请再次输入密码"
              class="iptHeight"
            >
              <template #prefix>
                <icon icon="icon-password" />
              </template>
            </el-input>
          </el-form-item>
          <el-form-item style="width: 100%">
            <el-button
              :loading="loading.btns"
              type="primary"
              class="btnSubmit"
              @click.native.prevent="resetPassword"
            >
              <span v-if="!loading.btns">重 置 密 码</span>
              <span v-else>重 置 中...</span>
            </el-button>
          </el-form-item>
          <div class="boxBottom">
            <div class="btnBack" @click="() => $emit('backToLogin')">返回登录</div>
          </div>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { UserActionEnum } from '@/store/modules/user/state';
import { defineComponent, onMounted, reactive, ref } from 'vue';
import { useStore } from 'vuex';
import { encrypt } from '@/utils/rsaEncrypt.js'

import VerificationCode from './verificationCode.vue'

defineComponent({
  name: 'RebackPasswordComponent'
})
const emit = defineEmits(['backToLogin']);

const store = useStore();

let ruleForm = reactive({
  phone: '',
  code: '',
  password: '',
  newPassword: '',
  verificationCode: '',
})
let loading = reactive({
  btns: false,
  code: false,
})

const validateNewPassword = (rule, value, callback) => {
  if (value !== ruleForm.password) {
    callback(new Error('两次输入密码不一致!'))
  } else {
    callback()
  }
}
const rules = {
  phone: [{ required: true, trigger: 'blur', message: '手机号不能为空' }],
  code: [{ required: true, trigger: 'blur', message: '验证码不能为空' }],
  password: [
    { required: true, trigger: 'blur', message: '密码不能为空' },
    {
      pattern: /^[~!@#$%^&*\-+=_.0-9a-zA-Z]{8,16}$/,
      message: '8-16密码数字英文混合',
    },
  ],
  newPassword: [
    { required: true, trigger: 'blur', message: '请再次输入密码' },
    { validator: validateNewPassword, trigger: 'blur' },
  ],
}
const graphicCode = ref('')
const ruleFormRef = ref();
function resetPassword () {
  ruleFormRef.value.validate((valid) => {
    if (valid) {
      loading.btns = true
      const data = {
        phone: encrypt(ruleForm.phone),
        code: encrypt(ruleForm.code),
        password: encrypt(ruleForm.password),
        newPassword: encrypt(ruleForm.newPassword),
      }
      store.dispatch(UserActionEnum.RESET_PASSWORD, data)
        .then(() => {
          ElMessage.success('重置成功')
          emit('backToLogin')
        })
        .catch(() => {
        }).finally(() => {
          loading.btns = false
        })
    } else {
      return false
    }
  })
}

function loadImage () {
  graphicCode.value = `${import.meta.env.VITE_BASE_URL}/code/captcha?` + Math.random()
}

onMounted(() => {
  graphicCode.value = `${import.meta.env.VITE_BASE_URL}/code/captcha`
})

</script>

<style lang="scss" scoped>
.loginBoxs {
  width: 602px;
  .topback {
    font-weight: 700;
    font-size: 42px;
    color: #231815;
    line-height: 59px;
    text-align: center;
    margin-bottom: 19px;
  }
  .box-card{
    border-radius: 10px;
  }
  .login-form {
    background: #ffffff;
    padding: 25px 25px 5px 25px;
  }
}

:deep(.iptHeight) {
  .el-input__wrapper {
    background: #F2F3F6;
    border-color: #FFFFFF;
    height: 60px;
    padding: 0px;
    box-shadow: none;
    .el-input__prefix{
      margin-left: 16px;
    }
    .el-input__inner {
      height: 100%;
      padding: 0 11px;
      border-radius: 3px;
    }
  }
  .el-input-group__append {
    .login-code {
      height: 100%;
      .el-button {
        height: 100%;
      }
    }
  }
}
:deep(.graphic) {
  .el-input-group__append {
    background: none;
    padding: 0 10px;
    img {
      width: 80px;
      height: 50px;
    }
  }
}
.boxBottom{
  display: flex;
  justify-content: flex-end;
  .btnBack{
    font-size: 14px;
    color: #979CAF;
    cursor: pointer;
    &:hover{
      color: #FF5000;
    }
  }
}
.btnSubmit{
  width: 100%;
  height: 74px;
  border-radius: 6px;
  background: #FF5000;
  border-width: 0;
  font-weight: 600;
  font-size: 24px;
  color: #FFFFFF;
  &:hover{
    background-color: #FBC548;
  }
}
</style>